package campus.service.impl;

import campus.domain.CampusSystemModule;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import campus.repository.CampusSystemModuleRepository;
import campus.service.CampusSystemModuleService;
import campus.service.dto.CampusSystemModuleDto;
import campus.service.dto.CampusSystemModuleQueryCriteria;
import campus.service.mapper.CampusSystemModuleMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author 李诗莹
* @date 2020-03-13
*/
@Service
//@CacheConfig(cacheNames = "campusSystemModule")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class CampusSystemModuleServiceImpl implements CampusSystemModuleService {

    private final CampusSystemModuleRepository campusSystemModuleRepository;

    private final CampusSystemModuleMapper campusSystemModuleMapper;

    public CampusSystemModuleServiceImpl(CampusSystemModuleRepository campusSystemModuleRepository, CampusSystemModuleMapper campusSystemModuleMapper) {
        this.campusSystemModuleRepository = campusSystemModuleRepository;
        this.campusSystemModuleMapper = campusSystemModuleMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(CampusSystemModuleQueryCriteria criteria, Pageable pageable){
        Page<CampusSystemModule> page = campusSystemModuleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(campusSystemModuleMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<CampusSystemModuleDto> queryAll(CampusSystemModuleQueryCriteria criteria){
        return campusSystemModuleMapper.toDto(campusSystemModuleRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public CampusSystemModuleDto findById(Integer id) {
        CampusSystemModule campusSystemModule = campusSystemModuleRepository.findById(id).orElseGet(CampusSystemModule::new);
        ValidationUtil.isNull(campusSystemModule.getId(),"CampusSystemModule","id",id);
        return campusSystemModuleMapper.toDto(campusSystemModule);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public CampusSystemModuleDto create(CampusSystemModule resources) {
        return campusSystemModuleMapper.toDto(campusSystemModuleRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(CampusSystemModule resources) {
        CampusSystemModule campusSystemModule = campusSystemModuleRepository.findById(resources.getId()).orElseGet(CampusSystemModule::new);
        ValidationUtil.isNull( campusSystemModule.getId(),"CampusSystemModule","id",resources.getId());
        campusSystemModule.copy(resources);
        campusSystemModuleRepository.save(campusSystemModule);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            campusSystemModuleRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<CampusSystemModuleDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CampusSystemModuleDto campusSystemModule : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("模块标签名称", campusSystemModule.getModuleLableName());
            map.put("模块id，如果该条数据是模块可忽略", campusSystemModule.getLastModuleId());
            map.put("类型，这里表示属于模块还是标签", campusSystemModule.getType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}